package com.vcs.web;

import java.io.File;

import com.vcs.dao.JobseekerDAO;
import com.vcs.dao.JobseekerDAOImpl;
import com.vcs.domain.*;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ResumeUploadAction extends ActionSupport implements SessionAware {

	private static final Log log = LogFactory.getLog(ResumeUploadAction.class);

	private String contentType;
	private File upload;
	private String fileName;
	private String caption;
	JobseekerDAO jdao = new JobseekerDAOImpl();
	// session map
	Map<String, Object> sessionMap;

	public String execute() {
		log.info("execute()...");
		// JobseekerDAO jdao = new JobseekerDAOImpl();

		try {
			// 1. create file name fistname_lastname_id.doc
			String firstName = ((Jobseeker) sessionMap.get("js"))
					.getFirstName();
			String lastName = ((Jobseeker) sessionMap.get("js")).getLastName();
			String id = ((Jobseeker) sessionMap.get("js")).getJobseekerId()
					.toString();
			fileName = firstName + "_" + lastName + "_" + id
					+ ".doc".toLowerCase();
			log.info("Constructed target file name is ======> " + fileName);
			// 2. Check operating system and construct directory structure

			String opSys = System.getProperty("os.name"); // find OS
			log.info("Operating system is: [" + opSys + "]");
			
			String workingDir = "downloads"; // directory name
			
			File dir = new File(workingDir);
			if(!dir.exists()){
				boolean status = new File(workingDir).mkdir();
			}else{
				log.info("directory not created. already exists!");
			}
			
			String fullFileName = null;

			if (opSys.equalsIgnoreCase("Linux")) {
				
				fullFileName = "." + File.separator + workingDir
						+ File.separator + fileName;
			} else {
				fullFileName = File.separator + workingDir + File.separator
						+ fileName;
			}

			log.info("Full file name: [" + fullFileName+"]");
			File theFile = new File(fullFileName);
			FileUtils.copyFile(upload, theFile);
			
			// 3. Save resume name in database
			jdao.updateResumeName(fileName, Long.parseLong(id));
			// 4. Also update session "js" object with the same resume name and upload date
			((Jobseeker) sessionMap.get("js")).setResumeName(fileName);
			((Jobseeker) sessionMap.get("js")).setResumeUploadDate(new java.util.Date());
			log.info("Resume uploaded successfully");
			
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("Resume Upload Failed");
			log.info("Resume Upload Failed");
			return INPUT;
		}
		return SUCCESS;
	}

	// since we are using <s:file name="upload" .../> the file name will be
	// obtained through getter/setter of <file-tag-name>FileName
	public String getUploadFileName() {
		return fileName;
	}

	public void setUploadFileName(String fileName) {
		this.fileName = fileName;
	}

	// since we are using <s:file name="upload" ... /> the content type will be
	// obtained through getter/setter of <file-tag-name>ContentType
	public String getUploadContentType() {
		return contentType;
	}

	public void setUploadContentType(String contentType) {
		this.contentType = contentType;
	}

	// since we are using <s:file name="upload" ... /> the File itself will be
	// obtained through getter/setter of <file-tag-name>
	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getCaption() {
		return caption;
	}

	public void setCaption(String caption) {
		this.caption = caption;
	}

	public String input() throws Exception {
		return SUCCESS;
	}

	public String upload() throws Exception {
		return SUCCESS;
	}

	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}
}
